package src.Fourteen;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class key extends JFrame {

    public key(){
        final JLabel label = new JLabel();
        label.setText("备注：");
        getContentPane().add(label, BorderLayout.WEST);
        final JScrollPane scrollPane = new JScrollPane();
        getContentPane().add(scrollPane,BorderLayout.CENTER);
        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextArea textArea = new JTextArea();
        textArea.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                System.out.println("此次输入的是："+e.getKeyChar()+"");
            }

            @Override
            public void keyPressed(KeyEvent e) {
                String keyText = KeyEvent.getKeyText(e.getKeyCode());
                if(e.isActionKey()){
                    System.out.println("您按下的是动作键："+keyText+"");
                }else {
                    System.out.println("您按下的是非动作键："+keyText+"");
                    int keyCode = e.getKeyCode();
                    switch (keyCode){
                        case KeyEvent.VK_CONTROL:
                            System.out.println(",Ctrl键被按下了");
                            break;
                        case KeyEvent.VK_ALT:
                            System.out.println(",Alt键被按下了");
                            break;
                        case KeyEvent.VK_SHIFT:
                            System.out.println(",Shift键被按下");
                            break;
                    }
                    System.out.println();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                String keyText = KeyEvent.getKeyText(e.getKeyCode());
                System.out.println("您释放的是："+keyText+"键");
                System.out.println();
            }
        });
        textArea.setLineWrap(true);
        textArea.setRows(3);
        textArea.setColumns(15);
        scrollPane.setViewportView(textArea);

    }

    public static void main(String[] args) {
        key k = new key();
    }
}
